package com.logicwin.aibala.services.net;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;

import android.util.Log;

class NetThread extends Thread
{

    public boolean running = false;

    public NetThread()
    {
        running = true;
    }

    public void run()
    {
        while (running)
        {
            try
            {

            } catch (Exception ex)
            {
                ex.printStackTrace();
            } finally
            {

            }
        }
    }
}

public class NetConnection
{
    static final String tag = "NetConnection";

    static NetThread netThread = null;

    public static void startNetThread()
    {
        netThread = new NetThread();
        netThread.start();
    }

    public static void stopNetThread()
    {
        if (netThread != null)
        {
            netThread.running = false;
            netThread.interrupt();
        }
    }


    public static boolean downLoadFile(String path, String fileName)
    {
        InputStream is = null;
        FileOutputStream fos = null;
        try
        {

            URL url = new URL(path);
//            HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();   
//            //根据响应获取文件大小 
//            int fileLength=urlcon.getContentLength(); 
            
            is = url.openStream();
            File file = new File(fileName);

            fos = new FileOutputStream(file, false);
            
            download(is, fos);

        } catch (Exception ex)
        {
            // MMView.result = ex.getMessage();
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public static void download(InputStream is, FileOutputStream fos)
    {
        try
        {
        	
            byte[] buff = new byte[256];
//            int count = 0;
            int hasRead = 0;
            while (true)
            {
                hasRead = is.read(buff);
//                count += hasRead;
                if (hasRead < 0)
                {
                    break;
                }
                fos.write(buff, 0, hasRead);
                
            }
        } catch (Exception ex)
        {
            ex.printStackTrace();
        } finally
        {
            try
            {
                if (is != null)
                {
                    is.close();
                }
                if (fos != null)
                {
                    fos.close();
                }
            } catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    }

    public static String httpGet(String path, String message)
    {
        InputStream is = null;
        String result = "";

        try
        {
        	String urlPath = null;
        	if (message==null){
        		urlPath = path;
        	}
        	else {
        		urlPath = path + "?" +message;
        	}
            Log.i("http", urlPath);
            URL url = new URL(urlPath);
            is = url.openStream();

            int hasRead = 0;
            int BUFF_LEN = 256;
            byte[] buff = new byte[BUFF_LEN];
            while (true)
            {
                hasRead = is.read(buff);
                if (hasRead < 0)
                    break;
                result += new String(buff, 0, hasRead);
            }

        } catch (Exception ex)
        {
            ex.printStackTrace();
        } finally
        {
            if (is != null)
                try
                {
                    is.close();
                } catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        return result.trim();
    }

    public static String httpPost(String path, String message)
    {
        InputStream is = null;
        HttpURLConnection huc = null;
        OutputStream os = null;
        String result = "";

        try
        {
            URL url = new URL(path);
            is = url.openStream();
            huc = setURLConnectionProperties(url);
            if (message != null)
            {
                os = huc.getOutputStream();
                os.write(message.getBytes());
                os.flush();
            }
            int hasRead = 0;
            int BUFF_LEN = 256;
            byte[] buff = new byte[BUFF_LEN];
            while (true)
            {
                hasRead = is.read(buff);
                if (hasRead < 0)
                    break;
                result += new String(buff, 0, hasRead);
            }

        } catch (Exception ex)
        {
            ex.printStackTrace();
        } finally
        {
            try
            {
                if (os != null)
                {
                    os.close();
                }
                if (is != null)
                {
                    is.close();
                }
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return result.trim();
    }

    private static HttpURLConnection setURLConnectionProperties(URL url)
            throws IOException, ProtocolException
    {
        HttpURLConnection httpUrlConnection;
        URLConnection rulConnection = url.openConnection();
        httpUrlConnection = (HttpURLConnection) rulConnection;

        httpUrlConnection.setDoOutput(true);

        httpUrlConnection.setDoInput(true);

        httpUrlConnection.setUseCaches(false);

        httpUrlConnection.setRequestMethod("POST");
        
        try
        {
            httpUrlConnection.connect();
            httpUrlConnection.setConnectTimeout(1);
            httpUrlConnection.setReadTimeout(1);
        } catch (ConnectException e1)
        {
            if (e1.getMessage().equals("Connection refused: connect"))
            {

                System.exit(0);
            }
        }
        return httpUrlConnection;
    }

}

